/**
 * 2015-11-5
 * wayne
 */
package com.wayne.testapp.utils;

import android.content.Context;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * wayne
 * 2015-11-5 上午11:26:39
 */
public class FileUtils {
    private final static String FOLDER = "tttestzip";

    /**
     * 返回一个zip文件
     *
     * @return 2015-11-5 下午2:00:06
     * @throws IOException
     */
    public static File compress2Zip(File[] files, String filename) throws IOException {
        List<String> names = new ArrayList<String>();
        File dir = new File(CUtils.getSDPath() + File.separator + FOLDER);
        File zip = new File(CUtils.getSDPath() + File.separator + FOLDER, filename);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        if (!zip.exists()) {
            zip.createNewFile();
        }

        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
        for (File f : files) {
            String name = f.getName();
            if (names.contains(name)) {
                name = UUID.randomUUID() + "_" + name;
            }
            zos.putNextEntry(new ZipEntry(name));

            names.add(name);
            InputStream fis = new FileInputStream(f);
            byte content[] = new byte[1024];
            int datalen;
            while ((datalen = fis.read(content)) > 0) {
                zos.write(content, 0, datalen);
            }
            fis.close();
        }
        zos.closeEntry();
        zos.flush();
        zos.close();
        return zip;
    }

    public static File compress2Zip(Context ctx, File[] files, String filename) throws IOException {
        List<String> names = new ArrayList<String>();
        File zip = new File(CUtils.getSDPath() + File.separator + FOLDER, filename);

        if (!zip.exists()) {
            zip.createNewFile();
        }

        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
        for (File f : files) {
            String name = f.getName();
            if (names.contains(name)) {
                return null;
            }
            zos.putNextEntry(new ZipEntry(name));

            names.add(name);
            InputStream fis = new FileInputStream(f);
            byte content[] = new byte[1024];
            int datalen;
            while ((datalen = fis.read(content)) > 0) {
                zos.write(content, 0, datalen);
            }
            fis.close();
        }
        zos.closeEntry();
        zos.flush();
        zos.close();
        return zip;
    }

    /**
     * 转换json为文件
     *
     * @param json
     * @param filename
     * @return 2015-11-5 下午2:16:27
     * @throws IOException
     */
    public static File json2File(String json, String filename) throws IOException {
        File file = new File(CUtils.getSDPath() + "/" + FOLDER, filename);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter writer = new FileWriter(file.getAbsolutePath());
        writer.write(json.toString());
        writer.flush();
        writer.close();


        return file;
    }
}
